use dataset_voters, clear

tabulate year, gen(year_)

forvalues i = 1(1)4 {
	gen B`i' = .
	gen SE`i' = .
}
gen time = _n - 5

reg turnout f4lnwells lnwells l4lnwells ///
	year_2 year_3 year_4 year_5, a(zipcode) cl(zipcode)

replace B1 = _b[f4lnwells] if time == -4
replace SE1 = _se[f4lnwells] if time == -4
replace B1 = _b[lnwells] if time == 0
replace SE1 = _se[lnwells] if time == 0
replace B1 = _b[l4lnwells] if time == 4
replace SE1 = _se[l4lnwells] if time == 4

eststo: reg turnout f4anywells anywells l4anywells ///
	year_2 year_3 year_4 year_5, a(zipcode) cl(zipcode)

replace B2 = _b[f4anywells] if time == -4
replace SE2 = _se[f4anywells] if time == -4
replace B2 = _b[anywells] if time == 0
replace SE2 = _se[anywells] if time == 0
replace B2 = _b[l4anywells] if time == 4
replace SE2 = _se[l4anywells] if time == 4

reg turnout f4lnwells lnwells l4lnwells ///
	year_2 year_3 year_4 year_5 if highfracking == 1 ///
	, a(zipcode) cl(zipcode)

replace B3 = _b[f4lnwells] if time == -4
replace SE3 = _se[f4lnwells] if time == -4
replace B3 = _b[lnwells] if time == 0
replace SE3 = _se[lnwells] if time == 0
replace B3 = _b[l4lnwells] if time == 4
replace SE3 = _se[l4lnwells] if time == 4

eststo: reg turnout f4anywells anywells l4anywells ///
	year_2 year_3 year_4 year_5 if highfracking == 1 ///
	, a(zipcode) cl(zipcode)

replace B4 = _b[f4anywells] if time == -4
replace SE4 = _se[f4anywells] if time == -4
replace B4 = _b[anywells] if time == 0
replace SE4 = _se[anywells] if time == 0
replace B4 = _b[l4anywells] if time == 4
replace SE4 = _se[l4anywells] if time == 4

********************************************************************************
* collapse and plot
********************************************************************************

keep if !mi(B1)

collapse B* SE*, by(time)

forvalues i=1(1)4 {
	gen UB`i' = B`i' + 1.96*SE`i'
	gen LB`i' = B`i' - 1.96*SE`i'
}

forvalues i=1(1)4 {

	if `i' == 1 local mytitle "Log wells / all states"
	if `i' == 2 local mytitle "Any wells / all states"
	if `i' == 3 local mytitle "Log wells / h.f. states"
	if `i' == 4 local mytitle "Any wells / h.f. states"

	#delimit;

	gr tw
		(sc B`i' time, col(black))
		(rspike UB`i' LB`i' time, col(black))
		,
			yline(0)
			xline(-2, lpat(dash))
			ylab(, angle(horiz))
			legend(off)
			plotregion(style(none))
			name(g`i', replace)
			xtitle("")
			ytitle("")
			title("`mytitle'")
			;
			
	#delimit cr

}

gr combine g1 g2 g3 g4, ycommon xcommon b1title("Lead/lag in years") ///
	l1title("Estimate and 95% C.I.") imargin(zero)

gr export "_output/figureA3.pdf", replace
